package com.xidian.zhiancloud.controller.riskcontrol;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.riskcontrol.TriskWarSingManaStaList;
import com.xidian.zhiancloud.model.riskcontrol.entity.TWarSingManaStaEntity;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.riskcontrol.TriskWarSingManaStaService;
import com.xidian.zhiancloud.util.api.ExcelUtil2;
import com.xidian.zhiancloud.util.tools.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 风险管控-风险警示标志管理
 */
@RestController
@RequestMapping("/trwsms")
public class TriskWarSingManaStaController extends BaseController {

    @Autowired
    TriskWarSingManaStaService triskWarSingManaStaService;

    /**
     * 风险警示标志管理列表
     * @param reqdata  参数集合
     * @param isCondition 状态
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getTriskWarSingManaStaList")
    public ResultDTO getTriskWarSingManaStaList(BaseReqQuery reqdata,
           @RequestParam(value = "isCondition",required = false) String isCondition){
        Long companyId=this.getSessionUserCompanyId();
        Page<TriskWarSingManaStaList> pagedata = triskWarSingManaStaService
                .getTriskWarSingManaStaList(reqdata,isCondition,companyId);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("datalist", pagedata);
        data.put("PageNum", pagedata.getPageNum());
        data.put("PageSize", pagedata.getPageSize());
        data.put("PageTotal", pagedata.getPages());
        data.put("total", pagedata.getTotal());
        return ResultDTO.successOf(data, GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险警示标志管理添加
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/addTriskWarSingManaSta")
    public ResultDTO addTriskWarSingManaSta
        (TriskWarSingManaStaList tRiskWarSingManaSta){
        Long companyId=this.getSessionUserCompanyId();
        tRiskWarSingManaSta.setCompanyId(companyId);
        tRiskWarSingManaSta.setPersonAdd(this.getSessionUserid().intValue());
        triskWarSingManaStaService.addTriskWarSingManaSta(tRiskWarSingManaSta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险警示标志管理修改
     * @param id 风险警示标志管理Id
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/updateTriskWarSingManaSta")
    public ResultDTO updateTriskWarSingManaSta(
        @RequestParam("id") String id,
        TriskWarSingManaStaList tRiskWarSingManaSta){
        Long companyId=this.getSessionUserCompanyId();
        tRiskWarSingManaSta.setCompanyId(companyId);
        tRiskWarSingManaSta.setPersonAdd(this.getSessionUserid().intValue());
        triskWarSingManaStaService.updateTriskWarSingManaSta(id,tRiskWarSingManaSta);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险警示标志管理删除
     * @param id 风险警示标志管理ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/deleteTriskWarSingManaSta")
    public ResultDTO deleteTriskWarSingManaSta(@RequestParam("id") String id){
        triskWarSingManaStaService.deleteTriskWarSingManaSta(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 风险警示标志管理-查看
     * @param id 风险警示标志管理ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getTWarSingManaStaList")
    public ResultDTO getByTWarSingManaStaList(@RequestParam("id") String id){
        List<TriskWarSingManaStaList>  tWarSingManaSta=
                triskWarSingManaStaService.getByTWarSingManaStaList(id);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("tWarSingManaSta", tWarSingManaSta);
        return ResultDTO.successOf(data,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出风险警示标志管理
     * @param response
     * @param id 风险警示标志管理ID
     * @throws IOException 报错信息
     */
    @GetMapping("/exportTWarSingManaStaData")
    public void exportTWarSingManaStaData (HttpServletResponse response,
        @RequestParam(value="id",required=false ,defaultValue="0") String id)
            throws IOException {
        Long companyId=this.getSessionUserCompanyId();
        Page<TriskWarSingManaStaList> pagedata = triskWarSingManaStaService
                .getExportTWarSingManaStaData(companyId,id);
        List<TriskWarSingManaStaList> list = pagedata.getResult();
        ExcelUtil2<TriskWarSingManaStaList> util = new
                ExcelUtil2<TriskWarSingManaStaList>(TriskWarSingManaStaList.class);
        util.exportExcel(response, list, "风险警示标志管理");
    }

    /**
     * 导入风险警示标志管理
     * @param file 上传文件
     * @return
     */
    @RequestMapping("/importSingManaStaData")
    @ResponseBody
    public ResultDTO importSingManaStaData(@RequestParam MultipartFile file){
        Long companyId=this.getSessionUserCompanyId();
        Long userId=this.getSessionUserid();
        try{
            Object obj = TWarSingManaStaEntity.class.newInstance();
            List<Object> list = ExcelUtils.importBaseExcel0(file,
                "风险警示标志管理",obj);
            List<TWarSingManaStaEntity> wsiManList = new ArrayList<>();
            for(int i=0;i<list.size();i++) {
                TWarSingManaStaEntity singManaSta =
                    (TWarSingManaStaEntity)list.get(i);
                singManaSta.setTypeWarning(((TWarSingManaStaEntity) list.get(i)).getTypeWarning());
                singManaSta.setNameWarning(((TWarSingManaStaEntity) list.get(i)).getNameWarning());
                singManaSta.setLocale(((TWarSingManaStaEntity) list.get(i)).getLocale());
                singManaSta.setLiaPhone(((TWarSingManaStaEntity) list.get(i)).getLiaPhone());
                singManaSta.setAttachment(((TWarSingManaStaEntity) list.get(i)).getAttachment());
                wsiManList.add(singManaSta);
            }
            triskWarSingManaStaService.addSingManaStaEntity
                (companyId,userId,wsiManList);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.errorOf(UserResponseEnums.DATA_IMPORT_FAILED_ERROR);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

}
